<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Framework's Controller.
 */
class Controller extends BaseCallback
{
	public    $name;
	public    $action;
	public    $here;
#	public    $full_here;
	public    $load;
	public    $beforeFilter = array();
	
	public    $components  = array();
	public    $helpers     = array();
	
	// data
	public    $params;
	public    $data = array();
	
	// model
	public    $uses = '';
	public    $modelClass;
	
	// view
	public    $view;
	public    $viewPath;
	public    $layout;
	public    $defaultLayout = 'default';
	public    $autoRender = true;
	
	protected $_viewVars  = array();
	public    $_templates = '';
	
	public    $pageTitle;
	
	function __construct($url, $action, &$params)
	{
		$this->name       = str_replace('Controller', '', get_class($this));
		$this->action     = $action;
		$this->here       = $url;
#		$this->full_here  = PROTOCOL.$_SERVER['HTTP_HOST'].'/'.$url;
		
		$this->modelClass = empty($this->uses) ? Inflector::singularize($this->name) : $this->uses;
		$this->viewPath   = Inflector::underscore($this->name);
		
		# parameters
		$this->params =& $params;
		if (get_magic_quotes_gpc())
		{
			$this->params['url']  = cleanArray($_GET);
			$this->params['form'] = cleanArray($_POST);
		}
		else
		{
			$this->params['url']  =& $_GET;
			$this->params['form'] =& $_POST;
		}
		$this->params['ajax'] = (env('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest');
		
		# data
		if (isset($this->params['form']['data']))
			$this->data =& $this->params['form']['data'];
		
		# callbacks
		$this->bindCallback('beforeAction');
		$this->bindCallback('afterAction');
		$this->bindCallback('beforeRender');
		$this->bindCallback('afterRender');
	}
	
	# model (loaded when called)
	
	function __get($name)
	{
		if ($name == $this->modelClass)
		{
			$this->$name = $this->load->model($name);
			
			if (isset($this->params['args'][0]) and $this->params['args'][0] != 'void')
				$this->$name->id = $this->params['args'][0];
			
			return $this->$name;
		}
	}
	
	# view
	
	function set($left, $right=null)
	{
		if (!is_array($left))
			$this->_viewVars[$left] =& $right;
		else
			$this->_viewVars = array_merge($this->_viewVars, $left);
	}
	
	function render($action=null, $layout=null, $format=null)
	{
		# which action?
		$action = is_null($action) ? $this->action : $action;
		
		# which layout?
		if (is_null($layout))
		{
			if (empty($this->layout))
			{
				if (isset($this->params['url']['layout']))
					$layout = $this->params['url']['layout'];
				elseif (isset($this->params['bare']) and $this->params['bare'])
					$layout = 'bare';
				elseif (isset($this->params['ajax']) and $this->params['ajax'])
					$layout = 'ajax';
				else
					$layout = $this->defaultLayout;
			}
			else {
				$layout = $this->layout;
			}
		}
		
		# render
		$this->callback('beforeRender', &$action);
		if (!isset($this->view))
		{
			require_once FEATHER.'views'.DS.'view.php';
			$this->view = new View($this);
		}
		$this->view->render($action, $layout, $this->_viewVars, $format);
		$this->callback('afterRender');
		
		$this->autoRender = false;
	}
	
	# redirects & messaging
	
	function flash($mesg, $url='/', $class=null, $status=null)
	{
		if ($this->params['ajax'])
		{
			if (!is_null($status))
				HTTP::status($status);
			echo $mesg;
		}
		else
		{
			if (DEBUG < 2)
			{
				$this->Session->setflash($mesg, $class);
				HTTP::redirect($url, $status);
			}
			else
			{
				$this->set(array('url' => $url, 'message' => $mesg));
				$this->render('', 'flash');
			}
		}
		exit;
	}
	
	function message($title, $message=null)
	{
		$this->set(array('title' => $title, 'message' => $message));
		
		if (!isset($this->view))
			$this->_templates = 'message';
		else
			$this->view->templates = 'message';
		
		$this->render('message');
	}
	
	function beforeAction() {}
	function afterAction()  {}
	
	function beforeRender() {}
	function afterRender()  {}
}
?>